#include <ctype.h>
#include <string.h>

void * memset(void * s, int c, size_t n)
{
    char * p = s;

    while (n > 0)
    {
        *p = c;
        n--, p++;
    }
    return s;
}

void * memcpy(void * d, const void * s, size_t n)
{
    char * p = d;

    while (n > 0)
    {
        *p = *(char *)s;
        p++, (char *)s++, n--;
    }
    return d;
}

int memcmp(const void * s1, const void * s2, size_t n)
{
    char * p1;
    char * p2;

    if (n == 0)
        return 0;

    p1 = (char *)s1;
    p2 = (char *)s2;

    while (*p1 != *p2)
        {
        p1++, p2++;
        if (--n == 0)
            return 0;
        }

    return (*p1 - *p2);
}

size_t strlen(const char * s)
{
    const char * p = s;

    while (*p != '\0')
        {
        p++;
        }

    return (p - s);
}

int strcmp(const char * s1, const char * s2)
{
    while ((*s1 == *s2) && *s1 != 0)
        s1++, s2++;

    return (*s1 - *s2);
}

char * strcpy(char * s1, const char * s2)
{
    char * r;
    char * ps1;
    char * ps2;

    ps1 = s1;
    ps2 = (char *)s2;
    r = s1;

    while ((*ps1++ = *ps2++) != '\0')
        ;

    return (r);
}

char * strcat(char * s1, const char * s2)
{
    char * p = s1;

    while (*s1++ != '\0')
        ;

    s1--;

    while ((*s1++ = *s2++) != '\0')
        ;

    return (p);
}

char * strstr(const char * s1, const char * s2)
{
    size_t l1, l2, i;

    l1 = strlen(s1);
    l2 = strlen(s2);

    for (i = 0; i <= l1 - l2; i++)
        {
        if (memcmp(s1 + i, s2, l2) == 0)
            return (char *)s1 + i;
        }

    return NULL;
}

